/**
 * @author wcj
 * @Date 2021/6/6 9:40
 * @Version 1.0
 */
public class ConcurrencyTest {

    private static final int COUNT = 1000000000;

    public static void concurrencyTest() throws InterruptedException {
        long start = System.currentTimeMillis();
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                int a = 0;
                for (int i = 0; i < COUNT; i++) {
                    a += 5;
                }
            }
        });
        t1.start();
        int b = 0;
        for (int i = 0; i < COUNT; i++) {
            b--;
        }
        t1.join();  //让子线程执行完毕之后，再重新回到主线程
        long end = System.currentTimeMillis();
        System.out.println("Concurrency Time:" + (end - start));
    }

    public static void serialTest() {
        long start = System.currentTimeMillis();
        int a = 0;
        for (int i = 0; i < COUNT; i++) {
            a += 5;
        }

        int b = 0;
        for (int i = 0; i < COUNT; i++) {
            b--;
        }
        long end = System.currentTimeMillis();
        System.out.println("Serial time:" + (end - start));
    }

    /**
     * 并发在执行次数多的时候整体效率明显优于串行化执行
     *
     * @param args
     * @throws InterruptedException
     */
    public static void main(String[] args) throws InterruptedException {
        concurrencyTest();
        serialTest();
    }
}
